#include <QMessageBox>
#include <QDebug>
#include "mainwidget.h"
#include "ui_mainwidget.h"

MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWidget)
{

    ui->setupUi(this);
    ui->stackedWidget->setCurrentIndex(1);
    ui->labelShow->setText( "登 录");
    this->setWindowTitle( "易云帐");
    this->setMaximumHeight( 300);
    this->setMaximumWidth( 250 );
    this->setMinimumHeight(300);
    this->setMinimumWidth( 250 );
    socketLog = new QTcpSocket(this);
    socketReg = new QTcpSocket(this);
    workwindow = new Workwindow(this);
    /*
     * 此处为异常代码，程序执行到此出错，用于关闭主页后打开登录界面
    connect( this->workwindow , Workwindow::on_destroyed_Signal,
             [=]()
             {
                this->show();
                ui->lineEditLogAccount->setText(this->username);
             }

           );
    */
    connect( socketLog , QTcpSocket::connected , this , MainWidget::on_socketLog_connected );
    connect( socketReg , QTcpSocket::connected ,
           [=]()
           {
               //QMessageBox::information( this , "成功" , "注册套接字连接成功" );
               if( true == ui->lineEditRegAccount->text().isEmpty() ||
                   true == ui->lineEditRegPasswd->text().isEmpty() ||
                   true == ui->lineEditRegPasswdSure->text().isEmpty()
                 )
               {
                   QMessageBox::warning( this , "输入错误" , "请输入正确的用户信息" );
                   return;
               }
               if( ui->lineEditRegPasswd->text() != ui->lineEditRegPasswdSure->text() )
               {
                   QMessageBox::warning( this , "错误" , "两次密码不一致" );
                   return;
               }
               QString str = "1" + ui->lineEditRegAccount->text() + "#" + ui->lineEditRegPasswd->text();
               socketReg->write( str.toUtf8() );
               ui->lineEditRegPasswd->clear();
               ui->lineEditRegPasswdSure->clear();
           }

           );
    connect( socketLog , QTcpSocket::readyRead ,
           [=]()
           {
               //QMessageBox::information( this , "成功" , "登录套接字读取成功" );
               QByteArray bytes = socketLog->readAll();
               buff = QString( bytes );
               if( "10000" == buff )
               {
                   QMessageBox::information( this , "成功" , "登录成功" );
                   socketLog->disconnectFromHost();
                   workwindow->show();
                   this->hide();
                   workwindow->setUsername(this->username);
                   workwindow->setIp( this->IP );
                   qDebug() << "this->username:" << this->username;
               }
               if( "10001" == buff )
               {
                   QMessageBox::information( this , "失败" , "账号不存在或者密码错误" );
                   socketLog->disconnectFromHost();
               }
               qDebug() << buff;
           }
           );
    connect( socketReg , QTcpSocket::readyRead ,
           [=]()
           {
                //QMessageBox::information( this , "成功" , "注册套接字读取成功" );
                QByteArray bytes = socketReg->readAll();
                buff = QString( bytes );
                if( "11000" == buff )
                {
                    QMessageBox::information( this , "成功" , "注册成功" );
                    socketReg->disconnectFromHost();
                    ui->lineEditLogAccount->setText(ui->lineEditRegAccount->text());
                    ui->lineEditRegPasswd->clear();
                    ui->lineEditRegPasswdSure->clear();
                    ui->stackedWidget->setCurrentIndex(1);
                }
                if( "11001" == buff )
                {
                    QMessageBox::information( this , "失败" , "注册失败" );
                    socketReg->disconnectFromHost();
                }
                qDebug() << buff;
            }
           );
}

MainWidget::~MainWidget()
{
    delete ui;
}
//登录界面的注册按钮按下，切换到注册页面
void MainWidget::on_pushButtonLogReg_clicked()
{
    ui->lineEditLogPasswd->clear();
    ui->stackedWidget->setCurrentWidget( ui->stackedWidget->nextInFocusChain() );
    ui->lineEditRegAccount->setText(ui->lineEditLogAccount->text());
    ui->labelShow->setText( "注 册");
    ui->lineEditLogPasswd->clear();
}
//注册界面的登录按钮按下，切换到登录界面
void MainWidget::on_pushButtonRegLog_clicked()
{
    ui->lineEditRegPasswd->clear();
    ui->lineEditRegPasswdSure->clear();
    ui->lineEditLogAccount->setText(ui->lineEditRegAccount->text());
    ui->stackedWidget->setCurrentIndex(1);
    ui->labelShow->setText( "登 录");
    ui->lineEditRegPasswd->clear();
    ui->lineEditRegPasswdSure->clear();
}
//按下Ip地址输入按钮，则初始化IP变量
void MainWidget::on_pushButtonIp_clicked()
{
    IP = ui->lineEditIp->text();
    QMessageBox::information(this , "提示" , "IP地址初始化成功");
}
//登录界面的登录按钮被按下
void MainWidget::on_pushButtonLogLog_clicked()
{
    if( true == IP.isEmpty())
    {
        QMessageBox::warning( this , "警告" , "IP地址尚未初始化");
        return;
    }
    if( true == ui->lineEditLogAccount->text().isEmpty() || true == ui->lineEditLogPasswd->text().isEmpty() )
    {
        QMessageBox::warning( this , "警告" , "请输入账号和密码信息" );
        return;
    }
    socketLog->connectToHost(QHostAddress( IP ), 8888 );
}
//登录按钮按下，如果连接服务器成功，则执行以下槽函数
void MainWidget::on_socketLog_connected()
{
    //QMessageBox::information(this , "成功" , "登录套接字连接服务器成功" );
    if( true == ui->lineEditLogAccount->text().isEmpty() || true == ui->lineEditLogPasswd->text().isEmpty() )
    {
        QMessageBox::warning( this , "警告" , "请输入账号和密码信息" );
        return;
    }
    this->username = ui->lineEditLogAccount->text();
    QString str = "0" + ui->lineEditLogAccount->text() + "#" + ui->lineEditLogPasswd->text();
    qDebug() << str;
    socketLog->write( str.toUtf8() );
    ui->lineEditLogPasswd->clear();
}
//注册按钮被按下执行的动作
void MainWidget::on_pushButtonRegReg_clicked()
{
    if( true == IP.isEmpty() )
    {
        QMessageBox::warning( this , "警告" , "IP地址尚未初始化" );
        return;
    }
    if( true == ui->lineEditRegAccount->text().isEmpty() ||
        true == ui->lineEditRegPasswd->text().isEmpty() ||
        true == ui->lineEditRegPasswdSure->text().isEmpty()
      )
    {
        QMessageBox::warning( this , "输入错误" , "请输入正确的用户信息" );
        return;
    }
    if( ui->lineEditRegPasswd->text() != ui->lineEditRegPasswdSure->text() )
    {
        QMessageBox::warning( this , "错误" , "两次密码不一致" );
        return;
    }
    socketReg->connectToHost( QHostAddress( IP) , 8888);
}
